//
//  LYPgwbView.m
//  SZBank
//
//  Created by LuoYao on 16/7/12.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "LYPgwbView.h"
#import "LYBtnLabel.h"

@interface LYPgwbView ()<LYBtnLabelDelegate>
{
    NSArray *_dataSource;/**< 数据源 */
    CGRect _rowFrame;/**< 记录最后一个控件的位置 */
    NSMutableDictionary *_inputDic;/**< 问卷调查数据汇总 */
    NSMutableArray *_inputArray;/**< 选择题汇总 */
}
@property (nonatomic) UITableView *tableView;/**< 问卷调查 */
@property (nonatomic) UIScrollView *scrollView;/**< 问卷调查2 */
@property (nonatomic) UIView *headerView;/**< 调查问卷头部 */

@end

@implementation LYPgwbView

- (instancetype)initWithFrame:(CGRect)frame withArray:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        _inputDic = [NSMutableDictionary dictionary];
        _inputArray = [NSMutableArray array];
        
        _dataSource = array;
        
//        NSLog(@"%@",_dataSource);
        
        _rowFrame = CGRectMake(_cKwidth * 0.1 * 0.8, _cKheight * 0.15, _cKwidth * 0.8 * 0.8, _cKheight * 0.2);
        //背景
        self.backImageView.image = [LocalDataManager getImageWithSourceOfPath:@"小背景"];
        
        
        [self addSubview:self.scrollView];
        
        
        
        //头部视图
        
        UILabel  *centerTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _cKwidth * 0.8, 50)];
        centerTitle.center = CGPointMake(_cKwidth * 0.4, _cKheight * 0.1);
        centerTitle.text = @"调查问卷";
        centerTitle.font = [UIFont systemFontOfSize:30];
        centerTitle.textAlignment = NSTextAlignmentCenter;
        [self.scrollView addSubview:centerTitle];
        
        //调查者信息
        UIView *personInfo = [[UIView alloc] initWithFrame:CGRectMake(_cKwidth * 0.1 * 0.8, _cKheight * 0.15, _cKwidth * 0.8 * 0.8, _cKheight * 0.2)];
        personInfo.backgroundColor = [UIColor lightGrayColor];
        
        for (int i = 0; i < _dataSource.count; i ++) {
              static  NSInteger _index = 0;
              static  NSInteger areaIndex = 0;/**< 标记出现文本框的下标 */
            
            if (i < 4) {
                
                UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(i % 2 * personInfo.bounds.size.width * 0.49, personInfo.bounds.size.height * 0.1 + i / 2 * personInfo.bounds.size.height * 0.4, personInfo.bounds.size.width * 0.14, personInfo.bounds.size.height * 0.3)];
                lb.text = _dataSource[i][@"name"];
                lb.font = [UIFont systemFontOfSize:FONT_SIZE];
                
                UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(personInfo.bounds.size.width * 0.15 + i % 2 * personInfo.bounds.size.width * 0.49, personInfo.bounds.size.height * 0.1 + i / 2 * personInfo.bounds.size.height * 0.4, personInfo.bounds.size.width * 0.33, personInfo.bounds.size.height * 0.3)];
                tf.tag = 10000 + i;
                tf.font = [UIFont systemFontOfSize:FONT_SIZE];
                tf.background = [LocalDataManager getImageWithSourceOfPath:@"理财_未选中_菜单按钮"];
                [tf addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventEditingChanged];
                
                [personInfo addSubview:lb];
                [personInfo addSubview:tf];
            }
            [self.scrollView addSubview:personInfo];
            
            
            if (i >= 4) {
                
                if ([_dataSource[i][@"attributeType"] isEqualToString:@"select"]) {
                    
                    //title
                    UILabel *titlelb = [[UILabel alloc] initWithFrame:CGRectMake(_cKwidth * 0.1 * 0.8,CGRectGetMaxY(_rowFrame), _cKwidth * 0.8, 50)];
                    titlelb.text = [NSString stringWithFormat:@"%ld.%@",_index - 3,_dataSource[i][@"name"]];
                    titlelb.font = [UIFont systemFontOfSize:FONT_SIZE];
                    [self.scrollView addSubview:titlelb];
                    

                    LYBtnLabel *view = [[LYBtnLabel alloc] initWithFrame:CGRectMake(_cKwidth * 0.1 * 0.8, CGRectGetMaxY(titlelb.frame), _cKwidth * 0.8, ((NSArray *)_dataSource[i][@"attributeOptionList"]).count * 50) array:_dataSource[i][@"attributeOptionList"] nameId:_dataSource[i][@"id"]];
                    view.delegate = self;
                    _rowFrame = view.frame;
                    
                    [self.scrollView addSubview:view];
                    
                }
                
                //标记文本框项
                if ([_dataSource[i][@"attributeType"] isEqualToString:@"textarea"]) {
                    areaIndex = _index;
                    _index = _index - 1;
                   
                }
                //最后显示文本框选项
                if (i == _dataSource.count - 1) {
                    
                    //title
                    UILabel *titlelb = [[UILabel alloc] initWithFrame:CGRectMake(_cKwidth * 0.1 * 0.8,CGRectGetMaxY(_rowFrame), _cKwidth * 0.8, 50)];
                    titlelb.text = [NSString stringWithFormat:@"%ld.%@",areaIndex,_dataSource[areaIndex][@"name"]];
                    titlelb.font = [UIFont systemFontOfSize:FONT_SIZE];
                    [self.scrollView addSubview:titlelb];
                    
                    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(_cKwidth * 0.1 * 0.8, CGRectGetMaxY(titlelb.frame), _cKwidth * 0.8, 150)];
                    textView.backgroundColor = [UIColor colorWithPatternImage:[LocalDataManager getImageWithSourceOfPath:@"内容框背景"]];
                    textView.font = [UIFont systemFontOfSize:FONT_SIZE];
                    
                    _rowFrame  = textView.frame;
                    [self.scrollView addSubview:textView];
                    
                    
                    //提交按钮
                    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(_cKwidth * 0.1 * 0.8, CGRectGetMaxY(textView.frame) + 50, _cKwidth * 0.1, 40)];
                    [sureBtn setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财_未选中_菜单按钮"] forState:UIControlStateNormal];
                    [sureBtn setTitle:@"提交" forState:UIControlStateNormal];
                    [sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; ;
                    [sureBtn addTarget:self action:@selector(action_BtnClick:) forControlEvents:UIControlEventTouchUpInside];
                    
                    [self.scrollView addSubview:sureBtn];
                }
                
            }
            
            _index ++;
            
            if (_index > _dataSource.count) {
                _index = i + 1;
            }
        }
        
        
    }
    return self;
}



#pragma mark - 协议代理
- (void)btnLabel:(LYBtnLabel *)btnLabel inputDictionary:(NSDictionary *)inputDictionary{
    [_inputDic setObject:inputDictionary[@"string"] forKey:inputDictionary[@"nameId"]];
}



#pragma mark - 点击事件
- (void)action_BtnClick:(UIButton *)sneder{
    //遍历取出输入的值
    NSString *text;
    for (UIView *vc in self.scrollView.subviews) {
        if ([vc isKindOfClass:[UITextView class]]) {
            text = ((UITextView *)vc).text;
        }
    }
    [_inputDic setObject:text forKey:@"8"];
    
    
    if ([_inputDic count] < 12) {
        [self alertMessage];
        return;
    }
    
    
    if (((NSString *)_inputDic[@"8"]).length < 5) {
        [self alertMessage];
        return;
    }
    
    //
    NSArray *array = [_inputDic allValues];
    
    for (int i = 0; i < array.count; i ++) {
        if ([array[i] isEqualToString:@"NoSelected"]) {
            [self alertMessage];
            return;
        }
    }
    
    NSLog(@"%@",_inputDic);
    
    [LocalDataManager addAlertControllerWithControllerTitle:@"提示" controllerMessage:@"提交成功,感谢你的参与！" actionSureTitle:@"确定" actionCancelTile:nil actionSureBlock:nil actionCancelBlock:nil style:0];

}

#pragma mark - 文本框输入方法
- (void)valueChanged:(UITextField *)sender{
    //个人信息装进汇总字典
    [_inputDic setObject:sender.text forKey:_dataSource[sender.tag - 10000][@"name"]];
}


#pragma mark - 弹窗
- (void)alertMessage{
    [LocalDataManager addAlertControllerWithControllerTitle:@"提示" controllerMessage:@"问卷不完整！" actionSureTitle:@"确定" actionCancelTile:nil actionSureBlock:nil actionCancelBlock:nil style:0];
}


#pragma mark - getter

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    
        _scrollView.contentSize = CGSizeMake(_cKwidth, _cKheight * 4.0);
        
    }
    return _scrollView;
}
@end
